﻿using MySql.Data.MySqlClient;
using power_car.Tool;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace power_car.Forms
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if ("".Equals(user_name.Text))
            {
                MessageBox.Show("请输入用户名");
                return;
            }
            if ("".Equals(user_pwd.Text))
            {
                MessageBox.Show("请输入密码");
                return;
            }

            MySqlConn mySqlConn = new MySqlConn();
            MySqlConnection conn = mySqlConn.getMySqlCon();
            conn.Open();

            try
            {
                string M_query_sql = String.Format("SELECT * FROM `user` WHERE user_name = '{0}' AND user_pwd = '{1}'", user_name.Text, user_pwd.Text);
                MySqlCommand mysqlcom = new MySqlCommand(M_query_sql, conn);
                MySqlDataReader dataReader = mysqlcom.ExecuteReader();
                if(dataReader.HasRows)
                {
                    MessageBox.Show("登录成功");
                    conn.Close();
                    this.Hide();
                    HomeForm homeForm = new HomeForm();
                    homeForm.Show();
                } 
                else
                {
                    MessageBox.Show("用户名或密码错误");
                    conn.Close();
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
        }
    }
}
